/*
 * ! ******************************************************************************
 *
 * Created on 2022年10月1日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;

import com.google.common.collect.ArrayListMultimap;
import org.pentaho.di.ui.util.IoUtils;

/**
 * @ClassName: Py4jDictionary
 * @author yzbdl@yzbdl.ac.cn
 */
public class Py4jDictionary {

	private ArrayListMultimap<String, String> duoYinZiMap;

	private static final String PREFIX = "ui/";

	private static final String CONFIG_NAME = "py4j.dic";

	private static final String PINYIN_SEPARATOR = "#";

	private static final String WORD_SEPARATOR = "/";

	private volatile boolean inited;

	private Py4jDictionary() {

	}

	public void init() {
		if (inited) {
			return;
		}
		//System.out.println("******start load py4j config******");
		Enumeration<URL> configs = null;
		try {
			String fullName = PREFIX + CONFIG_NAME;
			//System.out.println(fullName);
			ClassLoader cl = Thread.currentThread().getContextClassLoader();
			configs = cl.getResources(fullName);
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.duoYinZiMap = parse(configs);
		inited = true;

		//System.out.println("******load py4j config over******");
		//System.out.println("py4j map key size:" + duoYinZiMap.keySet().size());
	}

	private ArrayListMultimap<String, String> parse(Enumeration<URL> configs) {
		ArrayListMultimap<String, String> duoYinZiMap = ArrayListMultimap.create(512, 16);
		if (configs != null) {
			while (configs.hasMoreElements()) {
				parseURL(configs.nextElement(), duoYinZiMap);
			}
		}
		return duoYinZiMap;
	}

	private void parseURL(URL url, ArrayListMultimap<String, String> duoYinZiMap) {
		//System.out.println("parse py4j dictionary:" + url.getPath());
		InputStream in = null;
		BufferedReader br = null;
		try {
			// new
			// InputStreamReader(MyApplication.mContext.getResources().getAssets().open(fileName),"UTF-8");
			in = url.openStream();
			br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {

				String[] arr = line.split(PINYIN_SEPARATOR);

				if (StringUtils.isNotEmpty(arr[1])) {
					String[] dyzs = arr[1].split(WORD_SEPARATOR);
					for (String dyz : dyzs) {
						if (StringUtils.isNotEmpty(dyz)) {
							duoYinZiMap.put(arr[0], dyz.trim());
						}
					}
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(String.format("load py4j config:%s error", url), e);
		} finally {
			IoUtils.closeQuietly(br);
			IoUtils.closeQuietly(in);
		}
	}

	ArrayListMultimap<String, String> getDuoYinZiMap() {
		return duoYinZiMap;
	}

	public static Py4jDictionary getDefault() {
		return SingletonHolder.INSTANCE;
	}

	private static class SingletonHolder {
		private static final Py4jDictionary INSTANCE = new Py4jDictionary();
	}
}
